package edu.yzu.testspringboot002.controller;



import edu.yzu.testspringboot002.po.ChatGroup;
import edu.yzu.testspringboot002.service.ChatGroupService;
import edu.yzu.testspringboot002.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chatGroup")
public class ChatGroupController {

    @Autowired
    private ChatGroupService chatGroupService;

    @PostMapping("/add")
    public void addChatGroup(@RequestBody ChatGroup chatGroup) {
        chatGroupService.addChatGroup(chatGroup);
    }

    @DeleteMapping("/delete/{id}")
    public void deleteChatGroupById(@PathVariable int id) {
        chatGroupService.deleteChatGroupById(id);
    }

    @PutMapping("/update")
    public void updateChatGroup(@RequestBody ChatGroup chatGroup) {
        chatGroupService.updateChatGroup(chatGroup);
    }

    @GetMapping("/{id}")
    public ChatGroup getChatGroupById(@PathVariable int id) {
        return chatGroupService.getChatGroupById(id);
    }

    @GetMapping("/all")
    public List<ChatGroup> getAllChatGroups() {
        return chatGroupService.getAllChatGroups();
    }


    //add a doctor  to the chat group
    @PostMapping("/addDoctor")
    public void addDoctorToChatGroup(@RequestParam int chatGroupId, @RequestParam int doctorId) {
        chatGroupService.addDoctorToChatGroup(chatGroupId, doctorId);
    }

    //create a chat group method,paraneter is the doctor id's List
    @PostMapping("/createNewChatGroup")
    public void createNewChatGroup(@RequestBody List<Integer> doctorIds, @RequestParam String groupName) {
        chatGroupService.createNewChatGroup(doctorIds, groupName);
    }

    //quey  chat groups by doctor id
    @GetMapping("/queryChatGroupsByDoctorId")
    public ResponseResult<List<ChatGroup>> queryChatGroupsByDoctorId(@RequestParam int doctorId) {
        List<ChatGroup> list= null;
        try {
            list = chatGroupService.queryChatGroupsByDoctorId(doctorId);
            return ResponseResult.ok(list);
        } catch (Exception e) {
            return ResponseResult.failed("queryChatGroupsByDoctorId failed"+e.getMessage());
            //throw new RuntimeException(e);
        }


    }






}